#include "StdFile.h"
#include <stdio.h>

MyStr ReadFromFile(const char *filePath)
{
    //初始化一个MyStr,方便作为返回值
    INITSTR(s);
    //打开需要读取的文件
    FILE *f = fopen(filePath,"r");
    if(f == NULL)
    {
        printf("文件打开失败:%d!\n",ferror(f));
        return s;
    }
    //得出文件大小
    fseek(f,0,SEEK_END);
    long FileSize = ftell(f);
    //将文件内部指针指向文件开头
    rewind(f);
    //为MyStr扩容
    ResizeString(&s,FileSize+1);
    //读取文件指针f所指向文件的内容并写入s
    if( fread(s.string,FileSize,1,f) == 0 && feof(f) != true)
    {
        printf("文件读取错误! \n");
        ClearString(&s);
        return s;
    }

    fclose (f);
    return s;
}

MyStrList GetLineFromFile(const char *filePath)
{
    MyStrList list;
    InitMyStringList(&list);

    FILE *f = fopen(filePath,"r");
    if(f == NULL)
    {
        printf("文件打开失败!\n");
        return list;
    }
    
    char c = 0;
    while((c = fgetc(f)) != EOF)
    {
        INITSTR(str);
        while(c != '\n' && c != EOF)
        {
            InsertChar(&str,c,str.len);
            c = fgetc(f);
        }

        //避免多余的空行
        if(str.len != 0)
        {
            list.InsertTail(&list,str.string);
            ClearString(&str);
        }
    }
    fclose(f);
    return list;
}

bool WriteToFIle(const char * filePath,const void *content,size_t size)
{
    FILE *f = fopen(filePath,"w");
    if(f == NULL)
    {
        printf("文件打开失败!\n");
        return false;
    }

    if( fwrite(content,size,1,f) == 0)
    {
        printf("write error:%d\n",ferror(f));
        fclose(f);
        return false;
    }
    fclose(f);
    return true;
}

bool AppendToFIle(const char * filePath,const void *content,size_t size)
{
    FILE *f = fopen(filePath,"a");
    if(f == NULL)
    {
        printf("文件打开失败!\n");
        return false;
    }

    if( fwrite(content,size,1,f) == 0)
    {
        printf("write error:%d\n",ferror(f));
        fclose(f);
        return false;
    }
    fclose(f);
    return true;
}

bool WriteStrListToFile(const char *filePath, MyStrList strlist)
{
    FILE *f = fopen(filePath,"a");
    if(f == NULL)
    {
        printf("文件打开失败!\n");
        return false;
    }    

    for(int i = 0; i < strlist.len; i++)
    {
        MyStr s =strlist.stringList[i];
        InsertString(&s,"\n");
        if(fwrite(s.string,s.len,1,f) == 0)
        {
            printf("write error:%d\n",ferror(f));
            fclose(f);
            return false;
        }
    }
    fclose(f);
    return true;
}

size_t GetFileSize(const char *filePath)
{

    FILE *f = fopen(filePath,"r");
    if(f == NULL)
    {
        printf("文件打开失败:%d!\n",ferror(f));
        return -1;
    }
    //得出文件大小
    fseek(f,0,SEEK_END);
    long FileSize = ftell(f);
    fclose(f);

    return FileSize;
}

void CreateNewFile(const char *filePath)
{
    FILE *f = fopen(filePath,"w");
    if(f == NULL)
    {
        return;
    }
    fclose(f);
}

bool IsFileExist(const char *filePath)
{
    FILE *f = fopen(filePath,"r");
    if(f == NULL)
    {
        return false;
    }
    fclose(f);
    return true;
}
